package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper,BaseCategoryTrademark> implements BaseCategoryTrademarkService {
    //  调用mapper 层
    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;
    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;


    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        //select * from base_category_trademark where category3_id = category3Id
        //先根据三级分类id获取品牌id
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id", category3Id);
        //需要的是trademark_id
        List<BaseCategoryTrademark> baseCategoryTrademarksList = baseCategoryTrademarkMapper.selectList(baseCategoryTrademarkQueryWrapper);

        if (!CollectionUtils.isEmpty(baseCategoryTrademarksList)) {
            List<Long> idList = baseCategoryTrademarksList.stream().map((baseCategoryTrademark) -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());
//            for (BaseCategoryTrademark baseCategoryTrademark : baseCategoryTrademarksList) {
//                Long trademarkId = baseCategoryTrademark.getTrademarkId();
//            }
            //返回品牌数据集合
            return baseTrademarkMapper.selectBatchIds(idList);
        }

        return null;
    }

    @Override
    public void remove(Long category3Id, Long trademarkId) {
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id", category3Id);
        baseCategoryTrademarkQueryWrapper.eq("trademark_id", trademarkId);

        baseCategoryTrademarkMapper.delete(baseCategoryTrademarkQueryWrapper);
    }

    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {
        //select * from base_category_trademark where category3_id = category3Id
        //先根据三级分类id获取品牌id
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id", category3Id);
        //需要的是trademark_id
        List<BaseCategoryTrademark> baseCategoryTrademarksList = baseCategoryTrademarkMapper.selectList(baseCategoryTrademarkQueryWrapper);

        if (!CollectionUtils.isEmpty(baseCategoryTrademarksList)) {
            //获取当前三级分类下所有品牌
            List<Long> idList = baseCategoryTrademarksList.stream().map((baseCategoryTrademark) -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());
            //在所有品牌中剔除已经上架的(返回可选中的品牌)
            return baseTrademarkMapper.selectList(null).stream().filter(baseTrademark->{
                return !idList.contains(baseTrademark.getId());
            }).collect(Collectors.toList());
        }
        //当前三级分类下没有品牌，返回所有品牌
        return baseTrademarkMapper.selectList(null);
    }

    @Override
    public void sava(CategoryTrademarkVo categoryTrademarkVo) {
        //把数据插入到中间表中建立联系就好base_category_trademark
        //获取品牌id
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        if (!CollectionUtils.isEmpty(trademarkIdList)) {
            //用来批量插入中间表
//            ArrayList<BaseCategoryTrademark> baseCategoryTrademarkList = new ArrayList<>();
//            for (Long tmId : trademarkIdList) {
//                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark;
//                baseCategoryTrademark.setTrademarkId(tmId);
//                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
//                baseCategoryTrademarkList.add(baseCategoryTrademark);
//            }

//              拉姆达表达式真好！
            List<BaseCategoryTrademark> baseCategoryTrademarkList = trademarkIdList.stream().map((tmId) -> {
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setTrademarkId(tmId);
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                return baseCategoryTrademark;
            }).collect(Collectors.toList());
            //批量插入
            this.saveBatch(baseCategoryTrademarkList);
        }
    }
}
